<template>

  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :rules="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="指派人" prop="assignor">
        <el-input
          v-model="queryParams.assignor"
          placeholder="请输入指派人"
          clearable
          size="small"
          maxlength="30"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="完成状态" prop="completeStatus">
        <el-select
          v-model="queryParams.completeStatus"
          placeholder="完成状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['server:task:add']"
        >新增
        </el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['server:task:remove']"
        >删除
        </el-button>
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['server:task:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" align="center" key="taskId" prop="taskId" v-if="columns[0].visible"/>
      <el-table-column label="指派人" align="center" key="assignor" prop="assignor" v-if="columns[1].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="任务地点" align="center" key="taskAddress" prop="taskAddress" v-if="columns[2].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="任务内容" align="center" key="taskContent" prop="taskContent" v-if="columns[3].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="预估完成时间" align="center" key="completeTime" prop="completeTime" v-if="columns[4].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="完成状态" align="center" key="completeStatus" prop="completeStatus" :formatter="statusFormat" v-if="columns[5].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['server:task:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['server:task:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改运维记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="指派人" prop="assignor">
          <el-input v-model="form.assignor" placeholder="请输入指派人" maxlength="30"/>
        </el-form-item>
        <el-form-item label="任务地点" prop="taskAddress">
          <el-input v-model="form.taskAddress" placeholder="请输入任务地点" maxlength="60"/>
        </el-form-item>
        <el-form-item label="任务内容" prop="taskContent">
          <el-input v-model="form.taskContent" placeholder="请输入任务内容" maxlength="200"/>
        </el-form-item>
        <el-form-item label="完成时间" prop="completeTime">
          <el-date-picker
            style="width: 360px;"
            v-model="form.completeTime"
            size="small"
            value-format="yyyy-MM-dd HH:mm"
            format="yyyy-MM-dd HH:mm"
            type="datetime"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="完成状态" prop="completeStatus">
          <el-select v-model="form.completeStatus" placeholder="请选择" style="width: 360px;">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="200"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>


import {
  addTask,
  delTask,
  exportTask,
  getTask,
  listTask,
  updateTask
} from "../../../api/server/estates/task";

export default {
  name: 'task',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运维表格数据
      taskList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否显示详细弹出层
      openView: false,
      // 状态字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assignor: undefined,
        completeStatus: undefined,
      },
      // 表单参数
      form: {
      },
      //列信息
      columns: [
        {key: 0, label: "序号", visible: true},
        {key: 1, label: "指派人", visible: true},
        {key: 2, label: "任务地点", visible: true},
        {key: 3, label: "任务内容", visible: true},
        {key: 4, label: "预估完成时间", visible: true},
        {key: 5, label: "完成状态", visible: true}
      ],
      queryForm: {
        assignor: [{ pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
      },
      // 表单校验
      rules: {
        assignor:[ {required: true, message: '指派人不能为空', trigger: 'blur'}],
        taskAddress: [ {required: true, message: '任务地点不能为空', trigger: 'blur'}],
        taskContent: [ {required: true, message: '任务内容不能为空', trigger: 'blur'}],
        completeTime: [ {required: true, message: '完成时间不能为空', trigger: 'blur'}],
        completeStatus:[ {required: true, message: '完成状态不能为空', trigger: 'change'}],
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('t_complete_status').then(response => {
      this.statusOptions = response.data
    })
  },
  methods: {
    /** 查询定时任务列表 */
    getList() {
      this.loading = true
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.completeStatus)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.$refs['queryForm'].validate(valid => {
        if (valid) {
          this.getList()
        }
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    reset() {
      this.form={
        assignor: "",
        taskAddress:"",
        taskContent:"",
        completeTime:"",
        completeStatus:"",
        remark:"",
      }
      this.resetForm('form')
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加运维任务'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.taskId || this.taskId
      getTask(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '查看运维任务'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.taskId != undefined) {
            updateTask(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addTask(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const taskIds = row.taskId || this.ids
      this.$confirm('是否确认删除该数据项?', '警告', {
        cancelButtonClass: 'btn-custom-cancel',
        closeOnClickModal: false,
        type: 'warning'
      }).then(function () {
        return delTask(taskIds);
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有运维任务表数据项?', '提示', {
        cancelButtonClass: 'btn-custom-cancel',
        closeOnClickModal: false,
        type: 'info'
      }).then(function () {
        return exportTask(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }
  }
}
</script>
